package com.lu.spzx.manager.service.impl;

import com.lu.spzx.manager.mapper.SysRoleUserMapper;
import com.lu.spzx.manager.service.SysRoleUserService;
import com.lu.spzx.model.dto.system.AssginRoleDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SysRoleUserServiceImpl implements SysRoleUserService {

    @Autowired
    private SysRoleUserMapper sysRoleUserMapper;

    @Transactional
    @Override
    public void doAssign(AssginRoleDto assginRoleDto) {

        // 删除之前的所有的用户所对应的角色数据
        this.sysRoleUserMapper.deleteByUserId(assginRoleDto.getUserId());

        // 分配新的角色数据
        List<Long> roleIdList = assginRoleDto.getRoleIdList();
        roleIdList.forEach(roleId -> {
            this.sysRoleUserMapper.doAssign(assginRoleDto.getUserId(), roleId);
        });
    }
}